/**
 * 
 */
package webreg.model;

import java.util.Iterator;
import java.util.Vector;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;

import webreg.model.exceptions.EmptyDescriptionException;

/** This class wraps age categories for athletics
 * 
 * @author STKNOLL
 */
@XmlAccessorType(XmlAccessType.NONE)
public class AgeTable implements Cloneable{

	/** table containing the age classifications */
	@XmlElementWrapper (name = "classificationTable")
	@XmlElement (name = "classification")	
	private Vector<AgeClassification> mTable;

	/** the name of the age table */
	@XmlAttribute (name = "name")	
	private String mName;

	/** Constructor for age table.
	 */
	public AgeTable () {
		mName = "";
		mTable = new Vector<AgeClassification>();
	}

	/** Constructor for age table with parameters
	 * 
	 * @param ageTable The age table to use for construction
	 */
	public AgeTable (Vector<AgeClassification> ageTable) {
		this();
		setTable(ageTable);
	}

	/** Constructor for age table with parameters
	 * 
	 * @param name The name of teh table
	 * @throws EmptyDescriptionException 
	 */
	public AgeTable (String name) throws EmptyDescriptionException {
		this();
		setName(name);
	}

	/** Constructor for age table with parameters
	 * 
	 * @param ageTable The age table to use for construction
	 * @param name The name of teh table
	 * @throws EmptyDescriptionException 
	 */
	public AgeTable (Vector<AgeClassification> ageTable, String name) throws EmptyDescriptionException {
		this(name);
		setTable(ageTable);
	}
	
	/** Copy constructor for age table
	 * 
	 * Private copy constructor for an age table
	 * 
	 * @param toCopy The age table to copy
	 * @throws EmptyDescriptionException 
	 */
	public AgeTable (AgeTable toCopy) {
		this();
		mTable = toCopy.mTable;
		mName = toCopy.mName;
	}
	
	/** Implementation of the equals method.
	 * 
	 * @param toCompare The age table to compare with
	 * @return True, if age table matches
	 */
	public boolean equals(AgeTable toCompare) {
		Iterator<AgeClassification> iter = mTable.iterator();
		Iterator<AgeClassification> iter2 = toCompare.mTable.iterator();
		for (; iter.hasNext() && iter2.hasNext(); ) {
			AgeClassification ageClass = (AgeClassification)iter.next();
			AgeClassification toCompareAgeClass = (AgeClassification)iter2.next();
			if (ageClass.equals(toCompareAgeClass) == false) return false;
		}
		if (this.mName.equals(toCompare.mName) == false) return false;
		return true;
	}

	
	/** Return the age classifications
	 * 
	 * @return The table with the age classifications
	 */
	public Vector<AgeClassification> getTable() {
		return mTable;
	}

	/** Sets the age classifications of this table
	 * 
	 * @param table The table with age classifications to set
	 */
	public void setTable(Vector<AgeClassification> table) {
		this.mTable = table;
	}

	/** Return the name of the age table
	 * @return The name of the age table 
	 */
	public String getName() {
		return mName;
	}
	
	/** Set the name of the age table
	 * @param name The name to set
	 * @throws EmptyDescriptionException 
	 */
	public void setName(String name) throws EmptyDescriptionException {
		if ( (name == null) || (name.trim().equals("") ) ) {
			throw new EmptyDescriptionException("AgeTable " + getName());
		}
		mName = name;
	}

	@Override
	public String toString() {
		return getName();
	}

	@SuppressWarnings("unchecked")
	@Override
	public Object clone() {
		AgeTable newTable = new AgeTable();
		newTable.mName = mName;
		newTable.mTable = (Vector<AgeClassification>) mTable.clone();
		return newTable;
	}

	/** Compare an age table against another one
	 * 
	 * @param other The age table to compare this one with
	 * @return -1 if this one is smaller, 0 if both are the same, 1 if this one is bigger
	 */
	public int compareTo(AgeTable other) {
		int nameCompare = mName.compareTo(other.mName);
		if ( nameCompare != 0) { return nameCompare; }
		
		Iterator<AgeClassification> iter = mTable.iterator();
		Iterator<AgeClassification> iter2 = other.mTable.iterator();
		for (; iter.hasNext() && iter2.hasNext(); ) {
			AgeClassification ageClass = (AgeClassification)iter.next();
			AgeClassification toCompareAgeClass = (AgeClassification)iter2.next();
			int ageClassCompare = ageClass.compareTo(toCompareAgeClass);
			if (ageClassCompare != 0) { return ageClassCompare; }
		}

		return 0;
	}

}
